using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DTO;

namespace DAL
{
    public class tbdanhmucDAL
    {
        
        private SqlConnection cn = null;
        public tbdanhmucDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
         public tbdanhmucDAL(tbdanhmucDAL thach)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
        public void Insert(tbdanhmuc danhmuc)
        {
            SqlCommand cm = new SqlCommand("danhmuc_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tendanhmuc", danhmuc.Tendanhmuc);
            cm.ExecuteNonQuery();
        }
        public void Update(tbdanhmuc danhmuc)
        {
            SqlCommand cm = new SqlCommand("danhmuc_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madanhmuc", danhmuc.Madanhmuc);
            cm.Parameters.AddWithValue("@tendanhmuc",danhmuc.Tendanhmuc);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("danhmuc_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madanhmuc", iD);
            cm.ExecuteNonQuery();
        }
        public tbdanhmuccollection Get(string where, int pageIndex, int pageSize,
           string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("danhmuc_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbdanhmuccollection danhmuc1Collection = new tbdanhmuccollection();
            while (dr.Read())
            {
                tbdanhmuc danhmuc = new tbdanhmuc();
                danhmuc.Madanhmuc = int.Parse(dr.GetValue(0).ToString());
                danhmuc.Tendanhmuc = dr.GetValue(1).ToString();
                danhmuc1Collection.Add(danhmuc);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return danhmuc1Collection;
        }
    }
}
